package com.android.settings.ios.face;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;

import androidx.preference.Preference;

import com.android.settings.ios.BaseFragment;
import com.android.settings.ios.R;
import com.android.settings.ios.view.OsSwitchPreference;

public class FaceIDFragment extends BaseFragment implements Preference.OnPreferenceClickListener {

    private boolean isPasswordOpened = false;
    private Preference mOpenPsdPref;
    private Preference mChangePsdPref;
    private Preference mAddFacePref;
    private OsSwitchPreference mSwitchUnlockPref;
    private OsSwitchPreference mSwitchApplePayPref;
    private OsSwitchPreference mSwitchAppStorePref;
    private OsSwitchPreference mSwitchAutoFillPref;

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.face_id_fragment);

        mSwitchUnlockPref = findPreference("face_pad_unlock");
        mSwitchApplePayPref = findPreference("face_apple_pay");
        mSwitchAppStorePref = findPreference("face_app_store");
        mSwitchAutoFillPref = findPreference("face_auto_fill");
        mOpenPsdPref = findPreference("face_id_open_psd");
        mChangePsdPref = findPreference("face_id_change_psd");
        mAddFacePref = findPreference("face_add_id");

        mSwitchUnlockPref.setOnPreferenceClickListener(this);
        mOpenPsdPref.setOnPreferenceClickListener(this);
        mChangePsdPref.setOnPreferenceClickListener(this);
        mAddFacePref.setOnPreferenceClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        isPasswordOpened = PasswordUtils.havePassword(getActivity());
        if (isPasswordOpened) {
            mOpenPsdPref.setTitle(R.string.os_face_close_password_title);
            mChangePsdPref.setEnabled(true);
            mSwitchUnlockPref.setChecked(Settings.System.getInt(getActivity().getContentResolver(), "iphone_face_unlock_enable", 0) == 1);
        } else {
            mOpenPsdPref.setTitle(R.string.os_face_set_password_title);
            mChangePsdPref.setEnabled(false);
            mAddFacePref.setEnabled(false);
            mSwitchUnlockPref.setChecked(false);
            mSwitchUnlockPref.setEnabled(false);
            mSwitchApplePayPref.setEnabled(false);
            mSwitchAppStorePref.setEnabled(false);
            mSwitchAutoFillPref.setEnabled(false);
        }
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        if (preference == mOpenPsdPref) {
            if (isPasswordOpened) {
                PasswordUtils.deletePassword(getActivity(), mOnDeletePasswordSuccessRunnable);
            } else {
                PasswordUtils.createPassword(getActivity(), mOnSetPasswordSuccessRunnable);
            }
        } else if (preference == mChangePsdPref) {
            PasswordUtils.verifyPassword(getActivity(), getString(R.string.os_face_change_password_title), new Runnable() {

                @Override
                public void run() {
                    PasswordUtils.createPassword(getActivity(), mOnSetPasswordSuccessRunnable);
                }
            }, null);
        } else if (preference == mSwitchUnlockPref) {
            if (mSwitchUnlockPref.isChecked()) {
                if (Settings.System.getInt(getActivity().getContentResolver(), "key_lock_type", 0) == 3/*LOCK_FACE*/) {
                    Settings.System.putInt(getActivity().getContentResolver(), "iphone_face_unlock_enable", 1);
                } else {
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.putExtra("open_lock", true);
                    intent.setClassName("com.android.systemui", "com.android.systemui.face.FaceGuideActivity");
                    getActivity().startActivity(intent);
                }
            } else {
                Settings.System.putInt(getActivity().getContentResolver(), "iphone_face_unlock_enable", 0);
            }
            return true;
        } else if (preference == mAddFacePref) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setClassName("com.android.systemui", "com.android.systemui.face.FaceGuideActivity");
            getActivity().startActivity(intent);
        }
        return false;
    }

    private final Runnable mOnSetPasswordSuccessRunnable = new Runnable() {

        @Override
        public void run() {
            isPasswordOpened = true;
            mOpenPsdPref.setTitle(R.string.os_face_close_password_title);
            mChangePsdPref.setEnabled(true);
            mAddFacePref.setEnabled(true);
            mSwitchUnlockPref.setEnabled(true);
            mSwitchApplePayPref.setEnabled(true);
            mSwitchAppStorePref.setEnabled(true);
            mSwitchAutoFillPref.setEnabled(true);
        }
    };

    private final Runnable mOnDeletePasswordSuccessRunnable = new Runnable() {

        @Override
        public void run() {
            isPasswordOpened = false;
            mOpenPsdPref.setTitle(R.string.os_face_set_password_title);
            mChangePsdPref.setEnabled(false);
            mAddFacePref.setEnabled(false);
            mSwitchUnlockPref.setChecked(false);
            mSwitchUnlockPref.setEnabled(false);
            mSwitchApplePayPref.setEnabled(false);
            mSwitchAppStorePref.setEnabled(false);
            mSwitchAutoFillPref.setEnabled(false);
        }
    };
}
